/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.helper;

import org.eclipse.uml2.uml.BehavioredClassifier;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Interface;
import org.eclipse.uml2.uml.InterfaceRealization;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.helper.IGraphEdgeHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Factory that can create and delete an {@link org.eclipse.uml2.uml.InterfaceRealization}.
 * 
 * @author Patrick Tessier
 */
public class EdgeInterfaceRealizationHelper extends EdgeDependencyHelper implements IGraphEdgeHelper{


	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.IGraphEdgeFactory#canExecute(com.cea.papyrus.diagraminterchange2.di2.GraphNode, com.cea.papyrus.diagraminterchange2.di2.GraphNode)
	 */	
	/**
	 * 
	 * 
	 * @param target 
	 * @param source 
	 * 
	 * @return 
	 */
	@Override
	public boolean canConnect(Element source,Element target){
		if (source==target){return false;}// generalization not on the same object
		if( source!=null&& (source instanceof org.eclipse.uml2.uml.Class)&& target==null){
			return true;
		}
		if ((source instanceof org.eclipse.uml2.uml.Class) && (target instanceof Interface)){// they are  classifiers
			return true;
		}
		return false;

	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.GraphEdgeFactory#canExecute(com.cea.papyrus.diagraminterchange2.di2.GraphNode)
	 */
	/**
	 * 
	 * 
	 * @param source 
	 * 
	 * @return 
	 */
	@Override
	public boolean canCreateGraphElement(GraphElement source){
		Element eltSource=null;
		if ((source instanceof Diagram)  ){
			if (((Uml1SemanticModelBridge)((Diagram)source).getOwner()).getElement()!=null){
				eltSource=((Uml1SemanticModelBridge)((Diagram)source).getOwner()).getElement();
				if (eltSource instanceof Package){return true;}
			}
		}
		if (source.getSemanticModel()!=null ){
			if (((Uml1SemanticModelBridge)source.getSemanticModel()).getElement()!=null){
				eltSource=((Uml1SemanticModelBridge)source.getSemanticModel()).getElement();
				if (eltSource instanceof  org.eclipse.uml2.uml.Package){// they are  classifiers
					return true;
				}

			}
		}

		return false;
	}


	///////////////////////////////////////////////////////////////////////////
	// UML Management
	///////////////////////////////////////////////////////////////////////////

	/**
	 * 
	 * 
	 * @param umlParent 
	 * @param name 
	 * 
	 * @return 
	 */
	public org.eclipse.uml2.uml.Element createUMLElement(String name, org.eclipse.uml2.uml.Element umlParent){
		Debug.debug(this,"create InterfaceRealization", channel); //$NON-NLS-1$
		InterfaceRealization ir = UMLFactory.eINSTANCE.createInterfaceRealization();
		ir.setName(name);
		((Package)umlParent).getPackagedElements().add(ir);
		return ir;		
	}


	/**
	 * 
	 * 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	public boolean canCreateUMLElement(Element umlParent) {
		if(umlParent instanceof Package) {
			return true;
		}
		return false;
	}


	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	public boolean addUMLElement(Element element, Element umlParent) {
		if(umlParent instanceof Package) {
			return ((Package)umlParent).getPackagedElements().add((PackageableElement)element);
		}
		return false;
	}


	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	public boolean removeUMLElement(Element element, Element umlParent) {
		if(umlParent instanceof Package) {
			return ((Package)umlParent).getPackagedElements().remove(element);
		}
		return false;
	}


	/**
	 * 
	 * 
	 * @return 
	 */
	public String getBaseString() {
		return "InterfaceRealization"; //$NON-NLS-1$
	}
	
	/**
	 * 
	 * 
	 * @param target 
	 * @param link 
	 * @param source 
	 * 
	 * @return 
	 */
	public boolean connect(Element source, Element target, Element link) {
		((InterfaceRealization)link).setName("Realization of "+((Classifier)target).getName());
		((InterfaceRealization)link).getSuppliers().clear();
		((InterfaceRealization)link).setContract((Interface)target);
		((BehavioredClassifier)source).getInterfaceRealizations().add((InterfaceRealization)link);
		return true;
	}
	
	/**
	 * 
	 * 
	 * @param target 
	 * @param link 
	 * @param source 
	 * 
	 * @return 
	 */
	public boolean deconnect(Element source, Element target, Element link) {
		((InterfaceRealization)link).setContract(null);
		((BehavioredClassifier)source).getInterfaceRealizations().remove(link);
		return true;
	}
	
}
